package com.rdxer.api;

import com.rdxer.api.para.IdsStringListPara;
import com.rdxer.core.entity.Result;
import com.rdxer.core.entity.StatusCode;
import com.rdxer.core.utils.search.SearchPara;
import com.rdxer.model.Course;
import com.rdxer.model.Major;
import com.rdxer.service.CourseService;
import com.rdxer.service.MajorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * departmentManagement
 */
@RestController
@CrossOrigin
@RequestMapping("/management/course")
public class CourseManageApi {

	@Autowired
	private CourseService service;
	
	
	/**
	 * 查询全部数据
	 * @return
	 */
	@GetMapping
	public Result<List<Course>> findAll(){
		return new Result<List<Course>>(true, StatusCode.OK,"查询成功",service.findAll());
	}
	
	/**
	 * 根据ID查询
	 * @param id ID
	 * @return
	 */
	@GetMapping("/{id}")
	public Result<Course> findById(@PathVariable String id){
		return new Result(true, StatusCode.OK,"查询成功",service.findById(id));
	}


	/**
	 * @param para
	 */
	@PostMapping("/doEdit")
	public Result save(@RequestBody Course para){
		Course data = service.save(para);
		Result<Course> result = new Result<>();
		result.setData(data);
		result.setMessage("操作成功");
		return result;
	}

	/**
	 * 修改
	 * @param department
	 */
	@PutMapping("/{id}")
	public Result<Course> update(@PathVariable String id, @RequestBody Course department){

		department.setId(id);

		Course data = service.update(department);

		Result<Course> result = new Result<>();
		result.setData(data);
		result.setMessage("修改成功");
		return result;
	}

	/**
	 * 删除
	 * @param ids
	 */
	// @DeleteMapping("/{id}")
	public Result<?> delete(@PathVariable IdsStringListPara ids){
		service.deleteByIds(ids.getIds());
		return new Result(true, StatusCode.OK,"删除成功");
	}

	//	 * 分页+多条件查询
	@PostMapping("/getList")
	public Result<Page<Course>> search(@Valid @RequestBody SearchPara para){
		Result<Page<Course>> result = new Result<>();
		Page<Course> data = service.search(para);
		result.setData(data);
		return result;
	}

	@PostMapping("/getAll")
	public Result<List<Course>> searchNoPage(@Valid @RequestBody SearchPara para){
		Result<List<Course>> result = new Result<>();
		List<Course> data = service.searchNoPage(para);
		result.setData(data);
		return result;
	}
}
